package com.gowants.pems.service;

import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gowants.pems.mapper.ParamsMapper;
import com.gowants.pems.model.Params;
import com.gowants.pems.util.DateUtil;
import com.gowants.pems.util.RedisUtils;

@Service(value="ParamsService")
public class ParamsService extends BaseService<Params> {

	@Autowired
	private ParamsMapper paramsMapper;
	@Autowired
	private RedisUtils redisUtils;
	/**
	 * key 前缀，防止和其他地方的key可以冲突
	 */
	@Value("${gowants.redisKey}")
	private String redisKey;
	/**
	 * 新增
	 */
	@Override
	public int save(Params record) {
		record.setId(UUID.randomUUID().toString().replace("-", ""));
		record.setCreateTime(DateUtil.getDate());
		String key=redisKey+"param:"+record.getNumber();
		Random r = new Random();
		boolean bo=redisUtils.setStringRedis(key, JSON.toJSON(record).toString(),
				r.nextInt(9999999));
		return paramsMapper.insert(record);
	}

	/**
	 * 根据ID删除
	 */
	@Override
	public int delete(String primaryKey) {
		int i=paramsMapper.deleteByPrimaryKey(primaryKey);
		if(i>0) {
			String key=redisKey+"param:*";
			Set<String> keys=redisUtils.keys(key);
	    	for(String key0:keys) {
	    		String spObject = redisUtils.getStringRedis(key0);
					if (StringUtils.isNotBlank(spObject)) {
						Params params=JSONObject.parseObject(spObject, Params.class);
						if(primaryKey.equals(params.getId())) {
							redisUtils.delStringRedis(key0);
						}
					}
	    	}
		}
		return i;
	}

	/**
	 * 根据ID检索
	 */
	@Override
	public int update(Params record) {
		record.setUpdateTime(DateUtil.getDate());
		int i=paramsMapper.updateByPrimaryKeySelective(record);
		if(i>0) {
			Params params=paramsMapper.selectByPrimaryKey(record.getId());
			String key=redisKey+"param:"+params.getNumber();
			Random r = new Random();
			boolean bo=redisUtils.setStringRedis(key, JSON.toJSON(params).toString(),
					r.nextInt(9999999));
		}
		return i;
	}

	/**
	 * 根据ID 查询
	 */
	@Override
	public Params selectOne(String primaryKey) {
		return paramsMapper.selectByPrimaryKey(primaryKey);
	}

	/**
	 * 根据系统字典名检索记录
	 */
	@Override
	public List<Params> select(Params record) {
		return paramsMapper.selectParamsByCondition(record);
	}

	@Override
	public int getCount(Params record) {
		return 0;
	}
	
	/**
	     * 符合条件的记录数
	 * @param params
	 * @return
	 */
	public int saveCheck(Params params) {
		return paramsMapper.getCount(params);
	}
	
	public String getParamValueByNumber(String primaryKey) {
		String key=redisKey+"param:"+primaryKey;
		String spObject = redisUtils.getStringRedis(key);
		if (StringUtils.isNotBlank(spObject)) {
			Params params=JSONObject.parseObject(spObject, Params.class);
			return params.getValue();
		}else {	
			Params params= paramsMapper.getParamByNumber(primaryKey);
			Random r = new Random();
			boolean bo=redisUtils.setStringRedis(key, JSON.toJSON(params).toString(),
					r.nextInt(9999999));
			return params.getValue();
		}
	}
	
	
	
	public Params getParamByNumber(String primaryKey) {
		String key=redisKey+"param:"+primaryKey;
		String spObject = redisUtils.getStringRedis(key);
		if (StringUtils.isNotBlank(spObject)) {
			Params params=JSONObject.parseObject(spObject, Params.class);
			return params;
		}else {	
			Params params= paramsMapper.getParamByNumber(primaryKey);
			Random r = new Random();
			boolean bo=redisUtils.setStringRedis(key, JSON.toJSON(params).toString(),
					r.nextInt(9999999));
			return params;
		}
	}
}
